package com.whiteape.book.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.common.RxEvent;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.dto.DownloadTaskBean;
import com.whiteape.book.model.dto.MediaChapterBean;
import com.whiteape.book.model.type.DownloadStatus;
import com.whiteape.book.ui.adapter.CatalogSelectAdapter;
import com.whiteape.book.ui.base.BaseActivity;
import com.whiteape.book.utils.DaoDbHelper;
import com.whiteape.book.utils.ViewUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MediaCatalogSelectActivity extends BaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    CatalogSelectAdapter mAdapter;
    List<MediaChapterBean> mData;
    List<MediaChapterBean> mSelectList;

    private static final String EXTRA_COLL_BEAN = "extra_coll_bean";
    private CollectionMediaBean mCollectionMediaBean;
    public static void startActivity(Context context, CollectionMediaBean collectionMediaBean) {
        context.startActivity(new Intent(context, MediaCatalogSelectActivity.class)
                .putExtra(EXTRA_COLL_BEAN, collectionMediaBean));
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mCollectionMediaBean = getIntent().getParcelableExtra(EXTRA_COLL_BEAN);
        mCollectionMediaBean.__setDaoSession(DaoDbHelper.getInstance().getSession());
        mData = mCollectionMediaBean.getMediaChapterList();
        mSelectList = new ArrayList<>();
    }


    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
//        toolbar.setLogo(R.mipmap.logo);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("章节列表");
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        mAdapter.setOnItemChildClickListener(((adapter, view, position) -> {
            MediaChapterBean bean = mData.get(position);
            if (bean.getDownloadStatus() == DownloadStatus.STATUS_FINISH) {
                return;
            }
            if (!mSelectList.contains(bean)) {
                mSelectList.add(bean);
            } else {
                mSelectList.remove(bean);
            }
            mAdapter.notifyItemChanged(position);
//            mAdapter.notifyDataSetChanged();
        }));

    }

    @Override
    protected void initView() {
        super.initView();
//        getWindow().getDecorView().setBackgroundColor(WaApp.getInstance().getResources().getColor(R.color.statusBarColor));
        mAdapter = new CatalogSelectAdapter(R.layout.item_chapter_for_download, mData);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

        recyclerView.setPadding((int) ViewUtils.dpToPixel(4, this), (int) ViewUtils.dpToPixel(10, this), (int) ViewUtils.dpToPixel(4, this), 0);
        mAdapter.notifyDataSetChanged();
        mAdapter.setSelectedList(mSelectList);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_chapter_select, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.chapter_all) {
            //全选
            if (mSelectList.size() == mData.size()) {
                mSelectList.clear();
            } else {
                mSelectList.clear();
                mSelectList.addAll(mData);
            }
            mAdapter.notifyDataSetChanged();
        } else if (id == R.id.chapter_sort) {
            Collections.reverse(mData);
            mAdapter.notifyDataSetChanged();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.fab_done)
    void onDoneButtonClick() {
        if (mSelectList.isEmpty()) {
            return;
        }
        DownloadTaskBean downloadTaskBean = new DownloadTaskBean();
        downloadTaskBean.setTaskName(mCollectionMediaBean.getId());
        downloadTaskBean.setCollectionMediaBean(mCollectionMediaBean);
        downloadTaskBean.setMediaId(mCollectionMediaBean.getId());
        downloadTaskBean.setBookChapters(mSelectList);
//                    downloadTaskBean.setLastChapter();

        //先保存，才会保存章节，而且下载的，都需要先收藏 ,必须在setBookChapters后面，因为里面会给chapter设定task_name
        MediaStorageManager.getInstance().updateCollectionMediaWithReplace(mCollectionMediaBean);

        MediaStorageManager.getInstance().saveDownloadTask(downloadTaskBean);
        RxBus.get().post(RxEvent.DOWNLOAD_TASK_ADD, downloadTaskBean);
        startActivity(new Intent(this, DownloadActivity.class));

    }

    @Override
    protected int getContentId() {
        return R.layout.activity_catalog_select;
    }


}
